package sockets;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class Client
{
    public static void main(String[] args)
    {
	String sumString = "";

	try
	{
	    if (args.length != 2)
	    {
		System.out
			.println("Passing arguments to the server <IP_host> <quantity>.");
		return;
	    }
	    System.out.println("Openning socket and creating stream.");
	    String host = args[0];
	    Socket socket = new Socket(host, 250);

	    DataOutputStream outputStream = new DataOutputStream(
		    socket.getOutputStream());
	    DataInputStream inputStream = new DataInputStream(
		    socket.getInputStream());

	    System.out.println("Initializing sum 0.");
	    outputStream.writeUTF("set_sum");
	    outputStream.flush();
	    sumString = inputStream.readUTF();

	    System.out.println("Increased.");
	    int count = new Integer(args[1]).intValue();
	    long startTime = System.currentTimeMillis();

	    for (int i = 0; i < count; i++)
	    {
		outputStream.writeUTF("Increasing.");
		outputStream.flush();
		sumString = inputStream.readUTF();
	    }

	    long stopTime = System.currentTimeMillis();
	    System.out.println("AVG ping = "
		    + ((stopTime - startTime) / (float) count) + "msecs.");
	    System.out.println("Counter = " + sumString + ".");
	}
	catch (Exception e)
	{
	    System.err.println(e);
	}
    }
}
